//
//  CHStringDrawingAdditions.m
//  Chocolat
//
//  Created by Pieter Omvlee on 8/14/08.
//  Copyright 2008 Insearchofaname. All rights reserved.
//

#import "CHStringDrawingAdditions.h"


@implementation CHStringAttributes

@synthesize font, color, alignment, fontSize;

- (id)initWithObjects:(id *)objects forKeys:(id *)keys count:(NSUInteger)count
{
  self = [super init];
  if (self) {
    attributes = [[NSMutableDictionary alloc] initWithObjects:objects forKeys:keys count:count];
  }
  return self;
}

- (id)init
{
  self = [super init];
  if (self != nil) {
    attributes = [[NSMutableDictionary alloc] init];
  }
  return self;
}

- (id)initWithDictionary:(NSDictionary *)dict
{
  self = [super init];
  if (self) {
    attributes = [[NSMutableDictionary alloc] initWithDictionary:dict];
    NSUserDefaults *defs = [[NSUserDefaultsController sharedUserDefaultsController] defaults];
    [attributes setValue:[NSNumber numberWithInt:[defs integerForKey:@"DisplayLigatures"]] forKey:NSLigatureAttributeName];
  }
  return self;
}

- (void) dealloc
{
  [attributes release];
  [super dealloc];
}

#pragma mark -

+ (id)stringAttributes
{
  return [[[self alloc] init] autorelease];
}

+ (id)stringAttributesWithFont:(NSFont *)aFont
{
  return [[[self alloc] initWithDictionary:[NSDictionary dictionaryWithObjectsAndKeys:aFont,NSFontAttributeName,nil]] autorelease];
}

+ (id)stringAttributesWithColor:(NSColor *)aColor
{
  return [[[self alloc] initWithDictionary:[NSDictionary dictionaryWithObjectsAndKeys:aColor,NSForegroundColorAttributeName,nil]] autorelease];
}

+ (id)stringAttributesWithFont:(NSFont *)aFont color:(NSColor *)aColor
{
  return [[[self alloc] initWithDictionary:
           [NSDictionary dictionaryWithObjectsAndKeys:aFont,NSFontAttributeName,aColor,NSForegroundColorAttributeName,nil]] autorelease];  
}

+ (id)stringAttributesWithFont:(NSFont *)aFont color:(NSColor *)aColor alignment:(NSTextAlignment)align
{
  CHStringAttributes *attrs = [self stringAttributesWithFont:aFont color:aColor];
  [attrs setAlignment:align];
  
  return attrs;
}

#pragma mark -

- (NSFont *)font
{
  return [attributes valueForKey:NSFontAttributeName];
}

- (NSInteger)fontSize
{
  return [[attributes valueForKey:NSFontAttributeName] pointSize];
}

- (NSColor *)color
{
  return [attributes valueForKey:NSForegroundColorAttributeName];
}

- (NSTextAlignment)alignment
{
  return [[attributes valueForKey:NSParagraphStyleAttributeName] alignment];
}

- (NSShadow *)shadow
{
  return [attributes valueForKey:NSShadowAttributeName];
}

#pragma mark -

- (void)setFont:(NSFont *)aFont
{
  [attributes setValue:aFont forKey:NSFontAttributeName];
}

- (void)setFontSize:(NSInteger)fontSize
{
  [attributes setValue:[[NSFontManager sharedFontManager] convertFont:[attributes valueForKey:NSFontAttributeName] toSize:fontSize]
                forKey:NSFontAttributeName];
}

- (void)setColor:(NSColor *)aColor
{
  [attributes setValue:aColor forKey:NSForegroundColorAttributeName];
}

- (void)setAlignment:(NSTextAlignment)align
{
  NSMutableParagraphStyle *style = [[[NSMutableParagraphStyle alloc] init] autorelease];
  [style setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]];
  [style setAlignment:align];
//  [style setHyphenationFactor:[[attributes valueForKey:NSParagraphStyleAttributeName] hyphenationFactor]];
  [attributes setValue:style forKey:NSParagraphStyleAttributeName];
}

- (void)disableHyphenation
{
  NSMutableParagraphStyle *style = [[[NSMutableParagraphStyle alloc] init] autorelease];
  [style setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]];
  [style setAlignment:[self alignment]];
  [style setHyphenationFactor:1.0];
  [attributes setValue:style forKey:NSParagraphStyleAttributeName];  
}

- (void)setShadow:(NSShadow *)shadow
{
  [attributes setValue:shadow forKey:NSShadowAttributeName];
}

- (id)copyWithZone:(NSZone *)zone
{
  return [[CHStringAttributes alloc] initWithDictionary:attributes];
}

- (void)setValue:(id)value forKey:(NSString *)key
{
  [attributes setValue:value forKey:key];
}

- (id)valueForKey:(NSString *)key
{
  return [attributes valueForKey:key];
}

- (void)setShadowOfColor:(NSColor *)color location:(int)loc
{
  NSShadow *shadow = [[NSShadow alloc] init];
  [shadow setShadowColor:color];
  [shadow setShadowOffset:NSMakeSize(0, loc == kShadowBelow ? -1 : +1)];
  [self setShadow:[shadow autorelease]];
}

- (void)setBigShadowOfColor:(NSColor *)color location:(int)loc
{
  NSShadow *shadow = [[NSShadow alloc] init];
  [shadow setShadowColor:color];
  [shadow setShadowOffset:NSMakeSize(0, loc == kShadowBelow ? -2 : +2)];
  [self setShadow:[shadow autorelease]];
}

#pragma mark -

- (NSUInteger)count
{
  return [attributes count];
}

- (id)objectForKey:(id)aKey
{
  return [attributes objectForKey:aKey];
}

- (NSEnumerator *)keyEnumerator
{
  return [attributes keyEnumerator];
}

@end

@implementation NSFont (CHFontAttributes)

- (CHStringAttributes *)stringAttributes
{
  return [CHStringAttributes stringAttributesWithFont:self];
}

- (CHStringAttributes *)stringAttributesWithColor:(NSColor *)aColor
{
  return [CHStringAttributes stringAttributesWithFont:self color:aColor];
}

@end

@implementation NSMutableAttributedString (CHMutableAttributedStringAdditions)
- (void)appendString:(NSString *)string attributes:(NSDictionary *)attributes
{
  [self appendAttributedString:[[[NSAttributedString alloc] initWithString:string attributes:attributes] autorelease]];
}
@end